package entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import java.util.Objects;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

@Entity
@Table(name = "event")
@NamedQueries({
    @NamedQuery(name = "Event.findAll", query = "SELECT e FROM Event e"),
    @NamedQuery(name = "Event.findByEventId", query = "SELECT e FROM Event e WHERE e.eventId = :eventId"),
    @NamedQuery(name = "Event.findByName", query = "SELECT e FROM Event e WHERE e.name = :name"),
    @NamedQuery(name = "Event.findByDate", query = "SELECT e FROM Event e WHERE e.date = :date"),
    @NamedQuery(name = "Event.findByScope", query = "SELECT e FROM Event e WHERE e.scope = :scope"),
    @NamedQuery(name = "Event.findByLocationType", query = "SELECT e FROM Event e WHERE e.locationType = :locationType"),
    @NamedQuery(name = "Event.findByCountry", query = "SELECT e FROM Event e WHERE e.country = :country"),
    @NamedQuery(name = "Event.findByCity", query = "SELECT e FROM Event e WHERE e.city = :city"),
    @NamedQuery(name = "Event.findByBadWeatherThreshold", query = "SELECT e FROM Event e WHERE e.badWeatherThreshold = :badWeatherThreshold")})
public class Event implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @NotNull
    @Column(name = "eventId")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer eventId;

    @Basic(optional = false)
    @NotNull(message = "Cannot be empty.")
    @Size(min = 2, max = 50)
    @Pattern(regexp = "[a-zA-Z0-9 ]*")
    @Column(name = "name")
    private String name;

    @Basic(optional = false)
    @NotNull(message = "Cannot be empty.")
    @Column(name = "date")
    @Temporal(TemporalType.TIMESTAMP)
    private Date date;

    @Basic(optional = false)
    @NotNull(message = "Cannot be empty.")
    @Size(min = 1, max = 20)
    @Column(name = "scope")
    private String scope;

    @Basic(optional = false)
    @NotNull(message = "Cannot be empty.")
    @Size(min = 1, max = 20)
    @Column(name = "locationType")
    private String locationType;

    @Basic(optional = false)
    @NotNull(message = "Cannot be empty.")
    @Size(min = 2, max = 40)
    @Pattern(regexp = "[a-zA-Z ]*")
    @Column(name = "country")
    private String country;

    @Basic(optional = false)
    @NotNull(message = "Cannot be empty.")
    @Size(min = 2, max = 40)
    @Pattern(regexp = "[a-zA-Z ]*")
    @Column(name = "city")
    private String city;

    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 20)
    @Column(name = "badWeatherThreshold")
    private String badWeatherThreshold;

    @ManyToMany(mappedBy = "events")
    private List<Calendar> calendars = new ArrayList<>();

    @JoinTable(name = "participate", joinColumns = {
        @JoinColumn(name = "eventId", referencedColumnName = "eventId")}, inverseJoinColumns = {
        @JoinColumn(name = "participantUsername", referencedColumnName = "username")})
    @ManyToMany
    private List<User> participants = new ArrayList<>();

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "event")
    private List<Notification> notifications = new ArrayList<>();

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "event")
    private List<Invitation> invitations = new ArrayList<>();

    @JoinColumn(name = "creatorUsername", referencedColumnName = "username")
    @ManyToOne(optional = false)
    private User creator;

    public Event() {
        badWeatherThreshold = WeatherType.NONE.toString();
    }

    public Event(Integer eventId) {
        this.eventId = eventId;
        badWeatherThreshold = WeatherType.NONE.toString();
    }

    public Event(Integer eventId, String name, Date date, String scope, String locationType, String country, String city, String badWeatherThreshold) {
        this.eventId = eventId;
        this.name = name;
        this.date = date;
        this.scope = scope;
        this.locationType = locationType;
        this.country = country;
        this.city = city;
        this.badWeatherThreshold = badWeatherThreshold;
    }

    public Integer getEventId() {
        return eventId;
    }

    public void setEventId(Integer eventId) {
        this.eventId = eventId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getScope() {
        return scope;
    }

    public void setScope(String scope) {
        this.scope = scope;
    }

    public String getLocationType() {
        return locationType;
    }

    public void setLocationType(String locationType) {
        this.locationType = locationType;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getBadWeatherThreshold() {
        return badWeatherThreshold;
    }

    public void setBadWeatherThreshold(String badWeatherThreshold) {
        this.badWeatherThreshold = badWeatherThreshold;
    }

    public List<Calendar> getCalendars() {
        return calendars;
    }

    public void setCalendars(List<Calendar> calendars) {
        this.calendars = calendars;
    }

    public void addCalendar(Calendar calendar) {
        calendars.add(calendar);
    }

    public void removeCalendar(Calendar calendar) {
        calendars.remove(calendar);
    }

    public List<User> getParticipants() {
        return participants;
    }

    public void setParticipants(List<User> participants) {
        this.participants = participants;
    }

    public void addParticipant(User participant) {
        participants.add(participant);
    }

    public void removeParticipant(User participant) {
        participants.remove(participant);
    }

    public List<Notification> getNotifications() {
        return notifications;
    }

    public void setNotifications(List<Notification> notifications) {
        this.notifications = notifications;
    }

    public void addNotification(Notification notification) {
        notifications.add(notification);
    }

    public void removeNotification(Notification notification) {
        notifications.remove(notification);
    }

    public List<Invitation> getInvitations() {
        return invitations;
    }

    public void setInvitations(List<Invitation> invitations) {
        this.invitations = invitations;
    }

    public void addInvitation(Invitation invitation) {
        invitations.add(invitation);
    }

    public void removeInvitation(Invitation invitation) {
        invitations.remove(invitation);
    }

    public User getCreator() {
        return creator;
    }

    public void setCreator(User creator) {
        this.creator = creator;
    }

    public boolean isPublic() {
        return EventScope.PUBLIC.toString().equalsIgnoreCase(scope);
    }

    public boolean isPrivate() {
        return EventScope.PRIVATE.toString().equalsIgnoreCase(scope);
    }

    public boolean isOutdoor() {
        return LocationType.OUTDOOR.toString().equalsIgnoreCase(locationType);
    }

    public boolean isIndoor() {
        return LocationType.INDOOR.toString().equalsIgnoreCase(locationType);
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (eventId != null ? eventId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof Event)) {
            return false;
        }
        Event other = (Event) object;
        return Objects.equals(this.eventId, other.eventId);
    }

    @Override
    public String toString() {
        return "entity.Event[ eventId=" + eventId + " ]";
    }
}
